package com.hospital.yang.login.controller;

import com.alibaba.fastjson.JSON;
import com.hospital.utils.RedisService;
import com.hospital.yang.login.entity.DoctorLogin;
import com.hospital.yang.login.service.DoctorLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @description：医生登录
 * @author：zou
 * @date：2021/9/26 15:55
 * @versions：v1.0
 */
@Controller
@RequestMapping("/doctorLogin")
public class DoctorLoginController {

    @Autowired
    private DoctorLoginService doctorLogin;
    @Autowired
    private RedisService redisService;

    /**
     * 进入登录页面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "doctorLogin/login";
    }

    /**
     * 登录验证
     * @param userName  用户名
     * @param userPwd   密码
     */
    @RequestMapping("/login")
    public String login(String userName, String userPwd, HttpServletRequest req) {
        boolean login = doctorLogin.login(userName, userPwd);
        if (login) {
            DoctorLogin doctor = doctorLogin.doctor(userName);
            //redis保存医生登录信息
            req.getSession().setAttribute("doctor", doctor);
            return "menu";
        } else {
            req.setAttribute("failing", "false");
            return "redirect:/doctorLogin/toLogin";
        }
    }

    @RequestMapping("/selIdCard")
    @ResponseBody
    public String selIdCard(String doctorId, String idCard) {
        boolean b = doctorLogin.forgetPwd(doctorId, idCard);
        return JSON.toJSONString(b);
    }

    @RequestMapping("/toUpdPwd")
    public String toUpdPwd() {
        return "doctorLogin/updatePwd";
    }

    @RequestMapping("/updPwd")
    public String updPwd(String doctorId, String pwd) {
        doctorLogin.forgetPwd(doctorId, pwd);
        return "doctorLogin/login";
    }

}
